package com.glodon.paas.document.web.resource.internal;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.glodon.paas.document.api.bean.File;
import com.glodon.paas.document.web.resource.DocumentResource;
import com.glodon.paas.document.web.util.SecurityUtils;

@Component
@Path("/internal/")
public class DocumentInternalResource extends DocumentResource {

    private static final Logger LOGGER = LoggerFactory.getLogger(DocumentInternalResource.class);

    @Override
    public String getUserId(HttpServletRequest request) {
        return SecurityUtils.getRequestUserId(request);
    }

    @Override
    public Logger getLoger() {
        return LOGGER;
    }

    @GET
    @Path("/cleanExpiredFiles")
    @Produces(MediaType.APPLICATION_JSON)
    public long cleanExpiredFiles(@Context HttpServletRequest request,
                                  @QueryParam("nDaysBefore") @DefaultValue("7") String nDaysBefore,
                                  @QueryParam("expiredFiles") @DefaultValue("100") String expiredFiles,
                                  @QueryParam("userId") String userId) {
        if (StringUtils.isEmpty(userId)) {
            userId = null;
        }
        return fileService.delExpiredFiles(userId, Integer.valueOf(nDaysBefore), Integer.valueOf(expiredFiles));
    }

    @GET
    @Path("/createDataBag")
    @Produces(MediaType.APPLICATION_JSON)
    public void createDataBag(@Context HttpServletRequest request, @QueryParam("fileId") String fileId) {
        fileService.createDataBag(fileId);
    }

    // @GET
    // @Path("/publishedDataBag")
    // @Produces(MediaType.APPLICATION_JSON)
    // public boolean publishedDataBag(@Context HttpServletRequest request, @QueryParam("databagId") String databagId
    // ) {
    // File publishedFile = fileService.getFileByDateBag(databagId);
    // return publishedFile != null && publishedFile.isPublished();
    // }
}
